<template>
   <div class="goods-comment">
      <div class="head">
         <div class="data">
            <p>
               <span>{{ commentInfo.salesCount }}</span>
               <span>人购买</span>
            </p>
            <p>
               <span>{{ commentInfo.praisePercent }}</span>
               <span>好评率</span>
            </p>
            <p>
               <span>{{ commentInfo.evaluateCount }}</span>
               <span>条评价</span>
            </p>
            <p>
               <span>{{ commentInfo.hasPictureCount }}</span>
               <span>条有图评价</span>
            </p>
         </div>
      </div>
      <!-- 排序 -->
      <div class="sort">
         <span>排序：</span>
         <a href="javascript:;"
          v-for="item in sortList"
          :key="item"
          @click="toggleSort(item.type)"
          :class="{active: item.type === reqParams.sortField}">{{item.name}}</a>
         <xtx-check-box style="margin-left:50px;" v-model="reqParams.hasPicture">是否有图</xtx-check-box>
      </div>
      <!-- 列表 -->
      <div class="list">
         <!-- 每一个评论数据 -->
         <div class="item" v-for="item in commentList" :key="item.id">
            <div class="user">
               <img :src="item.member.avatar" alt />
               <span>{{ item.member.nickname }}</span>
            </div>
            <div class="body">
               <div class="score">
                  <!-- 评分组件 -->
                  <!--
                     满星五星
                     score 是几  实心就有几个  剩下的就是空心
                  -->
                  <i class="iconfont icon-wjx01" v-for="(score,i) in item.score" :key="i"></i>
                  <i class="iconfont icon-wjx02" v-for="(score,i) in 5 - item.score" :key="i"></i>
                  <!-- 函数执行之后的返回值 -->
                  <span class="attr">{{ format(item.orderInfo.specs) }}</span>
               </div>
               <div class="text">{{ item.content }}</div>
               <!-- 预览组件 -->
               <good-comment-image :pictures="item.pictures"/>
               <div class="time">
                  <span>{{ item.createTime }}</span>
                  <span class="zan">
                     <i class="iconfont icon-dianzan"></i>
                     {{ item.praiseCount }}
                  </span>
               </div>
            </div>
         </div>
      </div>
   </div>
</template>
<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/good'
import { onMounted, ref, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodCommentImage from './good-comment-image.vue'
export default {
  name: 'GoodsComment',
  components: {
    GoodCommentImage
  },
  setup () {
    // 头部数据
    const commentInfo = ref({})
    const route = useRoute()
    async function loadData () {
      const res = await findCommentInfoByGoods(route.params.id)
      console.log(res)
      commentInfo.value = res.result
    }
    onMounted(() => {
      loadData()
    })

    // 使用基础筛选字段拉取数据
    const commentList = ref([])
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: false, // 是否有
      sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
    })
    async function loadList () {
      const res = await findCommentListByGoods(route.params.id, reqParams)
      commentList.value = res.result.items
    }
    onMounted(() => {
      loadList()
    })

    // 处理规格数据
    //  function format (specs) {
    //    console.log(specs)
    //    let str = ''
    //    // 编写处理逻辑: 把数组specs中的每一项里面的key+value 拼接到一起
    //    specs.forEach((item) => {
    //      str += `${item.name}:${item.nameValue} `
    //    })
    //    // reduce
    //    return str
    //  }
    function format (specs) {
      // 累加器函数 每一次遍历都会按照传入的累加逻辑进行累加处理 处理之后的值每次都会重新交给a
      // 直到全部遍历完毕之后 最终的累加值会作为返回值返回
      return specs.reduce((a, c) => a + `${c.name}:${c.nameValue} `, '')
    }
    // 排序功能实现
    const sortList = [
      {
        name: '默认',
        type: null
      },
      {
        name: '最热',
        type: 'praiseCount'
      },
      {
        name: '最新',
        type: 'createTime'
      }
    ]
    function toggleSort (type) {
      reqParams.sortField = type
    }

    watch(() => {
      return reqParams
    }, () => {
      loadList()
    }, {
      deep: true
    })

    return {
      commentInfo,
      commentList,
      format,
      sortList,
      toggleSort,
      reqParams
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
   .head {
      display: flex;
      padding: 30px 0;
      .data {
         flex: 1;
         display: flex;
         padding: 20px;
         p {
            flex: 1;
            text-align: center;
            span {
               display: block;
               &:first-child {
                  font-size: 32px;
                  color: @priceColor;
               }
               &:last-child {
                  color: #999;
               }
            }
         }
      }
   }
   .sort {
      height: 60px;
      line-height: 60px;
      border-top: 1px solid #f5f5f5;
      border-bottom: 1px solid #f5f5f5;
      margin: 0 20px;
      color: #666;
      > span {
         margin-left: 20px;
      }
      > a {
         margin-left: 30px;
         &.active,
         &:hover {
            color: @xtxColor;
         }
      }
   }
}
.list {
   padding: 0 20px;
   .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
         width: 160px;
         img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            overflow: hidden;
         }
         span {
            padding-left: 10px;
            color: #666;
         }
      }
      .body {
         flex: 1;
         .score {
            line-height: 40px;
            .iconfont {
               color: #ff9240;
               padding-right: 3px;
            }
            .attr {
               padding-left: 10px;
               color: #666;
            }
         }
      }
      .text {
         color: #666;
         line-height: 24px;
      }
      .time {
         color: #999;
         display: flex;
         justify-content: space-between;
         margin-top: 5px;
      }
   }
}
</style>
